package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDto;
import com.itheima.reggie.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@RequestMapping("/setmeal")
@RestController
@Api(tags = "套餐相关接口")
public class SetmealController {

    @Autowired
    SetmealService setmealService;

    @PostMapping
    @ApiOperation("新增套餐接口")
    private R<String> save(@RequestBody SetmealDto setmealDto, HttpSession session){
        Long empId = (Long) session.getAttribute("employee");
        setmealDto.setUpdateUser(empId);
        setmealDto.setCreateUser(empId);
        setmealService.save(setmealDto);
        return R.success("保存成功");
    }


    @GetMapping("/page")
    @ApiOperation("套餐分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",required = true),
            @ApiImplicitParam(name = "pageSize",value = "页面大小",required = true),
            @ApiImplicitParam(name = "name",value = "套餐名称",required = false)
    })
    public R<Page<SetmealDto>> page(Integer page,Integer pageSize,String name){
        Page<SetmealDto> pageResult = setmealService.page(page, pageSize, name);
        return R.success(pageResult);

    }

    @DeleteMapping
    @ApiOperation("套餐批量删除")
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.deleteByIds(ids);
        return R.success("删除成功");
    }

    @GetMapping("/list")
    @ApiOperation("套餐条件查询接口")
    public R<List<Setmeal>> list(Long categoryId,Integer status){
        List<Setmeal> setmealList = setmealService.list(categoryId, status);
        return R.success(setmealList);
    }

    @GetMapping("{id}")
    public R<SetmealDto> findById(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.findById(id);
        return R.success(setmealDto);

    }

    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable Integer status,@RequestParam List<Long> ids){
        setmealService.setStatus(status, ids);
        return R.success("状态修改成功");
    }

    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto, HttpSession session) {
        Long empId = (Long) session.getAttribute("employee");
        setmealDto.setUpdateUser(empId);
        setmealDto.setUpdateTime(LocalDateTime.now());
        setmealService.update(setmealDto);
        return R.success("修改成功");
    }

}
